<!-- eslint-disable vue/valid-v-slot -->
<!-- eslint-disable vue/no-lone-template -->
<template>
  <div class="main-container">
    <TableBody>
      <template #header>
        <TableHeader ref="tableHeaderRef" :show-filter="false">
          <template #search-content>
            <a-form ref="seachForm" :model="form">
              <a-row :gutter="24">
                <a-col :span="4">
                  <a-form-item field="card" hide-label>
                    <a-input
                      class="width100"
                      v-model="form.card"
                      placeholder="请输入卡号"
                      allow-clear
                    />
                  </a-form-item>
                </a-col>
                <a-col :span="4">
                  <a-form-item field="source" hide-label>
                    <a-select
                      class="width100"
                      v-model="form.source"
                      placeholder="请选择操作来源"
                      allow-clear
                    >
                      <a-option :value="0">系统内部</a-option>
                      <a-option :value="1">管理员后台</a-option>
                      <a-option :value="2">代理后台</a-option>
                      <a-option :value="3">充值前端</a-option>
                      <a-option :value="4">API</a-option>
                      <a-option :value="5">系统轮询</a-option>
                    </a-select>
                  </a-form-item>
                </a-col>
                <a-col :span="4">
                  <a-form-item field="type" hide-label>
                    <a-select
                      class="width100"
                      v-model="form.type"
                      placeholder="请选择操作类型"
                      allow-clear
                    >
                      <a-option :value="0">复机</a-option>
                      <a-option :value="1">停机</a-option>
                      <a-option :value="2">机卡绑定</a-option>
                      <a-option :value="3">删除实名</a-option>
                      <a-option :value="4">卡板限速</a-option>
                      <a-option :value="5">解除限速</a-option>
                      <a-option :value="6">实名变更</a-option>
                    </a-select>
                  </a-form-item>
                </a-col>

                <a-col :span="4">
                  <a-form-item field="success" hide-label>
                    <a-select
                      class="width100"
                      v-model="form.success"
                      placeholder="请选择操作状态"
                      allow-clear
                    >
                      <a-option :value="1">成功</a-option>
                      <a-option :value="0">失败</a-option>
                    </a-select>
                  </a-form-item>
                </a-col>
                <a-col :span="4">
                  <a-form-item field="addTime" hide-label>
                    <a-date-picker
                      class="width100"
                      v-model="form.addTime"
                      show-time
                      :disabledDate="changeTraceEnd"
                      placeholder="请选择开始操作时间"
                      :time-picker-props="{ defaultValue: '12:00:00' }"
                      format="YYYY-MM-DD HH:mm:ss"
                      :shortcuts="[
                        {
                          label: '今日',
                          value: () => (
                            (form.addTime = $dayjs('00:00:00', 'HH:mm:ss').format(
                              'YYYY-MM-DD HH:mm:ss'
                            )),
                            (form.endTime = $dayjs('23:59:59', 'HH:mm:ss').format(
                              'YYYY-MM-DD HH:mm:ss'
                            ))
                          ),
                        },
                        {
                          label: '昨日',
                          value: () => (
                            (form.addTime = $dayjs('00:00:00', 'HH:mm:ss')
                              .subtract(1, 'day')
                              .format('YYYY-MM-DD HH:mm:ss')),
                            (form.endTime = $dayjs('23:59:59', 'HH:mm:ss')
                              .subtract(1, 'day')
                              .format('YYYY-MM-DD HH:mm:ss'))
                          ),
                        },
                        {
                          label: '七日',
                          value: () => (
                            (form.addTime = $dayjs('00:00:00', 'HH:mm:ss')
                              .subtract(6, 'day')
                              .format('YYYY-MM-DD HH:mm:ss')),
                            (form.endTime = $dayjs().format('YYYY-MM-DD HH:mm:ss'))
                          ),
                        },
                        {
                          label: '本月',
                          value: () => (
                            (form.addTime = $dayjs('01 00:00:00', 'DD HH:mm:ss').format(
                              'YYYY-MM-DD HH:mm:ss'
                            )),
                            (form.endTime = $dayjs().format('YYYY-MM-DD HH:mm:ss'))
                          ),
                        },
                        {
                          label: '上月',
                          value: () => (
                            (form.addTime = $dayjs(`01 00:00:00`, 'DD HH:mm:ss')
                              .subtract(1, 'month')
                              .format('YYYY-MM-DD HH:mm:ss')),
                            (form.endTime = $dayjs()
                              .subtract(1, 'month')
                              .endOf('month')
                              .format('YYYY-MM-DD HH:mm:ss'))
                          ),
                        },
                      ]"
                    />
                  </a-form-item>
                </a-col>
                <a-col :span="4">
                  <a-form-item field="endTime" hide-label>
                    <a-date-picker
                      class="width100"
                      v-model="form.endTime"
                      show-time
                      :disabledDate="changeTraceStart"
                      placeholder="请选择结束操作时间"
                      :time-picker-props="{ defaultValue: '12:00:00' }"
                      :shortcuts="[
                        {
                          label: '今日',
                          value: () => (
                            (form.addTime = $dayjs('00:00:00', 'HH:mm:ss').format(
                              'YYYY-MM-DD HH:mm:ss'
                            )),
                            (form.endTime = $dayjs('23:59:59', 'HH:mm:ss').format(
                              'YYYY-MM-DD HH:mm:ss'
                            ))
                          ),
                        },
                        {
                          label: '昨日',
                          value: () => (
                            (form.addTime = $dayjs('00:00:00', 'HH:mm:ss')
                              .subtract(1, 'day')
                              .format('YYYY-MM-DD HH:mm:ss')),
                            (form.endTime = $dayjs('23:59:59', 'HH:mm:ss')
                              .subtract(1, 'day')
                              .format('YYYY-MM-DD HH:mm:ss'))
                          ),
                        },
                        {
                          label: '七日',
                          value: () => (
                            (form.addTime = $dayjs('00:00:00', 'HH:mm:ss')
                              .subtract(6, 'day')
                              .format('YYYY-MM-DD HH:mm:ss')),
                            (form.endTime = $dayjs().format('YYYY-MM-DD HH:mm:ss'))
                          ),
                        },
                        {
                          label: '本月',
                          value: () => (
                            (form.addTime = $dayjs('01 00:00:00', 'DD HH:mm:ss').format(
                              'YYYY-MM-DD HH:mm:ss'
                            )),
                            (form.endTime = $dayjs().format('YYYY-MM-DD HH:mm:ss'))
                          ),
                        },
                        {
                          label: '上月',
                          value: () => (
                            (form.addTime = $dayjs(`01 00:00:00`, 'DD HH:mm:ss')
                              .subtract(1, 'month')
                              .format('YYYY-MM-DD HH:mm:ss')),
                            (form.endTime = $dayjs()
                              .subtract(1, 'month')
                              .endOf('month')
                              .format('YYYY-MM-DD HH:mm:ss'))
                          ),
                        },
                      ]"
                    />
                  </a-form-item>
                </a-col>
                <a-col :span="4">
                  <a-form-item field="btns" hide-label>
                    <a-space :size="15" class="width100">
                      <a-button
                        v-permission="getOperationList"
                        type="primary"
                        html-type="submit"
                        @click="doRefresh(1)"
                        ><template #icon> <icon-search /> </template>搜索</a-button
                      >
                      <a-button v-permission="getOperationList" @click="resetSeach"
                        ><template #icon>
                          <icon-sync theme="outline" size="14" />
                        </template>
                        <!-- Use the default slot to avoid extra spaces -->
                        <template #default>重置</template></a-button
                      >
                    </a-space>
                  </a-form-item>
                </a-col>
              </a-row>
            </a-form>
          </template>
        </TableHeader>
      </template>
      <template #table-config>
        <a-space :size="15">
          <a-button
            type="outline"
            v-permission="exportOperationList"
            @click="exportExcelLists"
            :loading="loading"
          >
            <!-- Use the default slot to avoid extra spaces -->
            <template #default>导出</template>
          </a-button>
        </a-space>
        <a-space :size="15">
          <TableConfig
            @update-border="onUpdateBorder"
            @update-striped="onUpdateStriped"
            @refresh="doRefresh"
          />
        </a-space>
      </template>
      <template #default>
        <a-table
          :bordered="{ wrapper: true, cell: tables.bordered }"
          :stripe="(tables.striped as boolean)"
          :loading="tables.tableLoading"
          :data="table.dataList"
          :pagination="false"
          :column-resizable="true"
          :rowKey="rowKey"
          :columns="tableColumns.list"
          table-layout-fixed
          hide-expand-button-on-empty
          :default-expand-all-rows="true"
          :scroll="scroll"
        >
          <template #columns>
            <a-table-column
              v-for="item of tableColumns.list"
              :key="item.key"
              align="left"
              :title="(item.title as string)"
              :width="item.width"
              :data-index="(item.key as string)"
              :fixed="item.fixed"
              :ellipsis="true"
              :tooltip="true"
            >
              <template v-if="item.key === 'source'" #cell="{ record }">
                <a-space>
                  <a-tag v-if="record.source == 0">系统内部</a-tag>
                  <a-tag v-else-if="record.source == 1">管理员后台</a-tag>
                  <a-tag v-else-if="record.source == 2">代理后台</a-tag>
                  <a-tag v-else-if="record.source == 3">充值前端</a-tag>
                  <a-tag v-else-if="record.source == 4">API</a-tag>
                  <a-tag v-else-if="record.source == 5">系统轮询</a-tag>
                </a-space>
              </template>
              <template v-if="item.key === 'type'" #cell="{ record }">
                <a-space>
                  <a-tag v-if="record.type == 0">复机</a-tag>
                  <a-tag v-else-if="record.type == 1">停机</a-tag>
                  <a-tag v-else-if="record.type == 2">机卡绑定</a-tag>
                  <a-tag v-else-if="record.type == 3">删除官方实名</a-tag>
                  <a-tag v-else-if="record.type == 4">卡板限速</a-tag>
                  <a-tag v-else-if="record.type == 5">解除限速</a-tag>
                  <a-tag v-else-if="record.type == 6">实名变更</a-tag>
                </a-space>
              </template>
              <template v-if="item.key === 'success'" #cell="{ record }">
                <a-space>
                  <a-tag v-if="record.error" color="red">失败</a-tag>
                  <a-tag v-else color="arcoblue">成功</a-tag>
                </a-space>
              </template>
              <template v-if="item.key === 'error'" #cell="{ record }">
                <a-space>
                  <div v-if="!record.error">- - -</div>
                  <div v-else-if="record.error">{{ record.error }}</div>
                  <a-tooltip v-else-if="record.error.length > 19" :content="record.error">
                    <div
                      style="
                        width: 200px;
                        white-space: nowrap;
                        overflow: hidden;
                        text-overflow: ellipsis;
                        text-align: left;
                      "
                      >{{ record.error }}</div
                    >
                  </a-tooltip>
                </a-space>
              </template>
            </a-table-column>
          </template>
        </a-table>
      </template>
      <template #footer>
        <TableFooter ref="tableFooterRef" :pagination="pagination" />
      </template>
    </TableBody>
  </div>
</template>

<script lang="ts" setup>
  import { getOperationList, exportOperationList } from '@/api/url'
  import { get } from '@/api/http'
  import { usePagination, useRowKey, useTable } from '@/hooks/table'
  import { onMounted, reactive, ref } from 'vue'

  import useCommonStore from '@/store/modules/common'
  import _ from 'lodash-es'

  const table = useTable()

  const userCommon = useCommonStore()

  const pagination = usePagination(doRefresh)

  const seachForm = ref()

  const loading = ref(false)

  const admin: any = ref([])
  const scroll = {
    x: 1500,
  }

  const form = reactive({
    card: '',
    type: '',
    source: '',
    success: '', // 状态
    addTime: '',
    endTime: '',
  })
  const tables = reactive({
    tableLoading: false,
    bordered: false,
    striped: false,
    expand: [1],
    isExpand: false,
  })

  const tableColumns: any = reactive({
    list: [
      {
        title: '日志ID',
        key: 'id',
        dataIndex: 'id',
        align: 'center',
        checked: true,
        width: 100,
      },
      {
        title: '卡号',
        key: 'card',
        dataIndex: 'card',
        align: 'center',
        checked: true,
        width: 240,
      },
      {
        title: '操作来源',
        key: 'source',
        dataIndex: 'source',
        align: 'center',
        checked: true,
        width: 130,
      },
      {
        title: '操作类型',
        key: 'type',
        dataIndex: 'type',
        align: 'center',
        checked: true,
        width: 130,
      },
      {
        title: '状态',
        key: 'success',
        dataIndex: 'success',
        align: 'center',
        checked: true,
        width: 130,
      },
      {
        title: '操作说明',
        key: 'message',
        dataIndex: 'message',
        align: 'center',
        checked: true,
        width: 300,
      },
      {
        title: '失败原因',
        key: 'error',
        dataIndex: 'error',
        align: 'center',
        checked: true,
        width: 300,
      },
      {
        title: '操作时间',
        key: 'time',
        dataIndex: 'time',
        align: 'center',
        checked: true,
        width: 190,
      },
    ],
  })

  const rowKey = useRowKey('id')

  const resetSeach = () => {
    seachForm.value.resetFields()
    doRefresh(1)
  }
  const exportExcelLists = () => {
    loading.value = true
    userCommon
      .downLoads(exportOperationList, form, '卡板操作日志列表')
      .then(() => {
        loading.value = false
      })
      .catch(() => {
        loading.value = false
      })
  }

  function onUpdateBorder(isBordered: boolean) {
    tables.bordered = isBordered
  }

  function onUpdateStriped(isStriped: boolean) {
    tables.striped = isStriped
  }

  function doRefresh(value?: number) {
    tables.tableLoading = true
    if (value) {
      pagination.page = value
    }
    get({
      url: getOperationList,
      data: {
        ...form,
        page: pagination.page,
        size: pagination.pageSize,
      },
    }).then((res) => {
      tables.tableLoading = false
      table.handleSuccess(res.data.list)
      pagination.setTotalSize((res.data as any).totalSize)
      return res.data.list
    })
  }

  const adminLists = () => {
    userCommon.adminLists().then((res) => {
      admin.value = res.data
    })
  }

  //跟踪API开始时间限制结束时间
  function changeTraceEnd(time: any) {
    return time.getTime() > new Date(form.endTime).getTime() //大于结束时间的不选
  }
  //跟踪API结束时间限制开始时间
  function changeTraceStart(time: any) {
    return time.getTime() < new Date(form.addTime).getTime() //小于开始时间的不选
  }

  onMounted(async () => {
    await doRefresh()
    await adminLists()
  })
</script>

<style lang="less" scoped>
  :deep(.arco-table-cell-expand-icon) {
    justify-content: center;
  }

  .box {
    overflow: hidden; //超出的文本隐藏
    text-overflow: ellipsis; //用省略号显示
    white-space: nowrap;
  }

  .tabs {
    display: flex;
    justify-content: space-around;
    align-items: center;
    height: 40px;
    margin-bottom: 20px;

    .button {
      box-sizing: border-box;
      padding-bottom: 5px;
    }
  }

  .action {
    color: #009dff;
    border-bottom: 3px solid #009dff;
  }

  .butten {
    padding: 0px 4px;
  }
</style>
